package com.maszy.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.maszy.bean.MemberCard;
import com.maszy.service.MemberCardService;

public class MemberCardServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	MemberCardService service = new MemberCardService();
	String view = new String(); // 页面路径

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		MemberCardServlet servlet = new MemberCardServlet();
		RequestDispatcher rd = null;
		String method = request.getParameter("method");
		if ("queryAll".equals(method)) {
			// 查询出全部数据
			servlet.queryAll(request, response);
			view = "/background/membercard/membercard.jsp";
		} else if ("insert".equals(method)) {
			// 添加新记录
			servlet.add(request, response);
			// servlet.queryAll(request, response);
			/**
			 * 此处添加程序下载接口,将会员卡发送到所有收银机上.
			 * */
			view = "/background/membercard/membercard.jsp";
		} else if ("update".equals(method)) {
			// 修改记录
			servlet.update(request, response);
			servlet.queryAll(request, response);
			view = "/background/membercard/membercard.jsp";
		} else if ("delete".equals(method)) {
			// 删除记录
			servlet.delete(request, response);
			servlet.queryAll(request, response);
			view = "/background/membercard/membercard.jsp";
		} else if ("updateForward".equals(method)) {
			// 进入修改页面
			servlet.queryForOne(request, response);
			view = "/background/membercard/update_membercard.jsp";
		}

		try {
			rd = this.getServletContext().getRequestDispatcher(view);
			rd.forward(request, response);

		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void queryForOne(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("MemberCard_Id");
		if (!"".equals(id) && id != null) {
			MemberCard obj = new MemberCard();
			obj.setMemberCard_Id(id);
			obj = service.queryById(obj);
			request.setAttribute("obj", obj);
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("MemberCard_Id");
		if (!"".equals(id) && id != null) {
			MemberCard obj = new MemberCard();
			obj.setMemberCard_Id(id);
			service.delete(obj);
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		MemberCard obj = new MemberCard();
		String id = request.getParameter("MemberCard_Id");
		if (!"".equals(id) && id != null) {
			obj.setMemberCard_Id(id);
			obj.setMemberCard_Remark(request.getParameter("MemberCard_Remark"));
			service.update(obj);
		}
	}

	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub

	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		MemberCard obj = new MemberCard();
		String id1 = request.getParameter("MemberCard_Id1");
		String id2 = request.getParameter("MemberCard_Id2");
		String n_id = request.getParameter("n_id");

		long memberCard_Id1 = 0;
		if (!"".equals(id1) && id1 != null) {
			memberCard_Id1 = Long.parseLong(id1);
		}
		long memberCard_Id2 = 0;
		if (!"".equals(id2) && id2 != null) {
			memberCard_Id2 = Long.parseLong(id2);
		}
		int totalCount = 0;
		if (memberCard_Id1 > 0 && memberCard_Id2 > 0) {
			String a[] = n_id.split("[,]");		
			
			for (long i = memberCard_Id1; i <= memberCard_Id2; i++) {	
				
				//去除规则外的卡号
				if (!"".equals(n_id) && n_id != null&&a.length!=0) {			
					boolean b=false;					
					labelA:			
					
					for (int j = 0; j < a.length; j++) {
						// 排除不符合规则的会员卡号
						
						if (Long.toString(i).indexOf(a[j])<0) {							
								b=true;	
							
						}else{
							b=false;	
							break labelA;
						}
						
					}
					if(b){
						String card_id=Long.toString(i);
						if(card_id.length()<5){
							int n=5-card_id.length();
							for(int m=0;m<n;m++){
								card_id="0"+card_id;								
							}
						}
						obj.setMemberCard_Id(card_id);
						obj.setMemberCard_Status(Integer.parseInt(request
								.getParameter("MemberCard_Status")));
						String time = new SimpleDateFormat(
								"yyyy-MM-dd HH:mm:ss").format(Calendar
								.getInstance().getTime());
						obj.setMemberCard_OpeningHours(time);
						obj.setMemberCard_Remark(request
								.getParameter("MemberCard_Remark"));
						service.add(obj);
						totalCount++;	
					}
				}
				//如果没有规则
				else if(a[0]==""){
					String card_id=Long.toString(i);
					if(card_id.length()<5){
						int n=5-card_id.length();
						for(int m=0;m<n;m++){
							card_id="0"+card_id;								
						}
					}
					obj.setMemberCard_Id(card_id);
					obj.setMemberCard_Status(Integer.parseInt(request
							.getParameter("MemberCard_Status")));
					String time = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss").format(Calendar
							.getInstance().getTime());
					obj.setMemberCard_OpeningHours(time);
					obj.setMemberCard_Remark(request
							.getParameter("MemberCard_Remark"));
					//service.add(obj);
					totalCount++;
				}
			}
			request.setAttribute("TotalCount", totalCount);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}
}